package com.karry.yl.web.baseinfo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.yl.domain.baseinfo.Certificate;
import com.karry.yl.service.baseinfo.CertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机构资质荣誉controller
 */
@Controller
@RequestMapping("/cert")
public class CertificateController extends SuperController {

    @Autowired
    private CertificateService certificateService;

    /**
     * 到资质荣誉添加页面
     * @return
     */
    @RequestMapping
    public ModelAndView ui(){
        List<Certificate> certificates = certificateService.selectList(new EntityWrapper<Certificate>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        Map<String,Object> dataMap = new HashMap<>();
        if(null != certificates && certificates.size() > 0){
            for(Certificate cert : certificates){
                int type = cert.getType();
               if(type == Certificate.TYPE_YLZYXK){
                    dataMap.put("ylzyxk",cert);
                }else if(type == Certificate.TYPE_SPXK){
                    dataMap.put("spxk",cert);
                }else if(type == Certificate.TYPE_XJPD){
                    dataMap.put("xjpd",cert);
                }else if(type == Certificate.TYPE_YLSF){
                    List<Certificate> list;
                    if(!dataMap.containsKey("ylsf")){
                         list = new ArrayList<>();
                        dataMap.put("ylsf",list);
                    }else {
                        list = (List<Certificate>)dataMap.get("ylsf");
                    }
                    list.add(cert);
                }else if(type == Certificate.TYPE_CYSF){
                    List<Certificate> list;
                    if(!dataMap.containsKey("cysf")){
                        list = new ArrayList<>();
                        dataMap.put("cysf",list);
                    }else {
                        list = (List<Certificate>)dataMap.get("cysf");
                    }
                    list.add(cert);
                }else if(type == Certificate.TYPE_RYZS){
                    List<Certificate> list;
                    if(!dataMap.containsKey("ryzs")){
                        list = new ArrayList<>();
                        dataMap.put("ryzs",list);
                    }else {
                        list = (List<Certificate>)dataMap.get("ryzs");
                    }
                    list.add(cert);
                }else if(type == Certificate.TYPE_FWXY){
                    List<Certificate> list;
                    if(!dataMap.containsKey("fwxy")){
                        list = new ArrayList<>();
                        dataMap.put("fwxy",list);
                    }else {
                        list = (List<Certificate>)dataMap.get("fwxy");
                    }
                    list.add(cert);
                }
            }
        }
        return new ModelAndView("baseinfo/cert","dataMap",dataMap);
    }

    /**
     * 到资质荣誉添加页面
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JsonResponse save(Certificate cert){
        cert.setOrgId(currentUser.getOrgId());
        certificateService.save(cert);
        return JsonResponse.success(cert);
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        certificateService.delete(new EntityWrapper<Certificate>().eq("orgId",currentUser.getOrgId()).eq("id",id));
        return "redirect:/cert";
    }
}
